package ru.vsu.cs.pm.beans;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import ru.vsu.cs.pm.models.Item;
import ru.vsu.cs.pm.models.ItemType;
import ru.vsu.cs.pm.models.User;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Arrays;
import java.util.List;

/**
 * User: ohodyakov
 * Date: 27.12.2010
 * Time: 12:52:50
 */
@Component
public class ItemManagerImpl implements ItemManager {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    @Transactional(readOnly = true)
    public List<Item> getChildrenItems(Item parentItem) {
        if (parentItem == null || parentItem.getId() == null) {
            throw new NullPointerException("Parent item is not correct!");
        }
        List childrenItems = entityManager.createNamedQuery("item.get.children")
                .setParameter("userId", parentItem.getUser().getId())
                .setParameter("parentId", parentItem.getId())
                .getResultList();
        return childrenItems;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Item> getRootItemsForUser(User user) {
        List<ItemType> types = Arrays.asList(ItemType.INCOME, ItemType.EXPENSE);
        List rootItems = entityManager.createNamedQuery("item.get.roots.by.user.id.and.item.type")
                .setParameter("userId", user.getId())
                .setParameter("itemTypes", types)
                .getResultList();
        return rootItems;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Item> getRootItemsForUserByType(User user, ItemType itemType) {
        List rootItems = entityManager.createNamedQuery("item.get.roots.by.user.id.and.item.type")
                .setParameter("userId", user.getId())
                .setParameter("itemTypes", Arrays.asList(itemType))
                .getResultList();
        return rootItems;
    }

    @Override
    @Transactional(readOnly = true)
    public Item getItem(int itemId) {
        return entityManager.find(Item.class, itemId);
    }

    @Override
    @Transactional
    public Item saveOrUpdateItem(Item item) {
        Item updatedItem = null;
        if (item.getId() == null) {
            entityManager.persist(item);
            Item parentItem = item.getParent();
            if (parentItem != null) {
                parentItem.setChildrenCount(parentItem.getChildrenCount() + 1);
                entityManager.merge(parentItem);
            }
        } else {
            Item ItemToUpdate = getItem(item.getId());
            if (ItemToUpdate != null) {
                updatedItem = entityManager.merge(item);
            } else {
                //TODO: log warning
            }
        }
        return updatedItem == null ? item : updatedItem;
    }

    @Override
    @Transactional(readOnly = true)
    public Item getTransferItemForUser(User user) {
        if (user == null || user.getId() == null) {
            throw new IllegalArgumentException("User is null!");
        }
        return (Item) entityManager.createNamedQuery("item.get.by.user.id")
                .setParameter("userId", user.getId())
                .setParameter("isRootItem", true)
                .setParameter("itemTypes", Arrays.asList(ItemType.TRANSFER))
                .getSingleResult();
    }

    @Override
    @Transactional(readOnly = true
    )
    public Item getReserveItemForUser(User user) {
        if (user == null || user.getId() == null) {
            throw new IllegalArgumentException("User is null!");
        }
        return (Item) entityManager.createNamedQuery("item.get.by.user.id")
                .setParameter("userId", user.getId())
                .setParameter("isRootItem", true)
                .setParameter("itemTypes", Arrays.asList(ItemType.RESERVE))
                .getSingleResult();
    }
}
    